package com.scy.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@RestController
@RequestMapping("/text")
public class GetTextController {

    //application/xml;charset=UTF-8
    // text/xml;charset=UTF-8

    // text/html;charset=UTF-8
    // text/plain;charset=UTF-8
    @RequestMapping(value = "/getTextInfo", produces = "text/plain;charset=UTF-8")
//    @RequestMapping(value = "/getTextInfo")
    public String getHostInfo() {

        System.out.println("user.dir = " + System.getProperty("user.dir"));
        System.out.println("file.encoding = " + System.getProperty("file.encoding"));

        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader bufferedReader=null;
        try {
            inputStream = this.getClass().getClassLoader().getResourceAsStream("log4j2.xml");
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(System.lineSeparator());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
               e.printStackTrace();
            }
        }

        String string = stringBuilder.toString();
        System.out.println(string);

        return string;
    }
}
